#!/bin/bash

if [[ -z $1 || $1 == "-h" || $1 == "--help" ]]; then
    echo "Usage: $0 [git command]"
    echo "     -D/-d/--depth N status(N is for find -maxdepth N+1 ...)(default N is 1)"
    exit
fi


depth=1
if [[ $1 == "-D" || $1 == "-d" || $1 == "--depth" ]];then
    depth=$2
    shift 2
fi

succ=1
errors=()

function cmd()
{
    echo -e ">>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>\n"
    proj=`readlink -f $1/..`
    shift
    cmd="git $*"
    cd $proj && $cmd
    if [[ $? != 0 ]];then
        succ=0
        err="\e[31mat [$proj] exec [$cmd] failed\e[0m"
        errors+=("$err")
        echo -e $err
    else
        echo -e "\e[92mat [$proj] exec [$cmd] success\e[0m"
    fi
    echo -e "\n<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<\n\n\n\n\n"
}

# find
root=`readlink -f .`
if [[ $depth == 0 ]];then
    list=`find $root -iname ".git" -type d`
else
    list=`find $root -maxdepth $(( depth + 1 )) -iname ".git" -type d`
fi

# run
for p in $list;do
    cmd $p $*
done

# report
if [[ $succ == 1 ]];then
    echo -e "\e[92mall success\e[0m"
else
    echo -e "\e[31mhave some errors:\e[0m"
    for i in "${!errors[@]}"; do
        echo -e ${errors[i]}
    done
fi
echo -e "\e[0m"
